package com.baomidou.springwind.controller.city;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.kisso.annotation.Permission;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.springwind.controller.BaseController;
import com.baomidou.springwind.entity.*;
import com.baomidou.springwind.enums.NodeTypeEnum;
import com.baomidou.springwind.pojo.DrivingRoute;
import com.baomidou.springwind.pojo.Test;
import com.baomidou.springwind.service.FlowAnalysisService;
import com.baomidou.springwind.service.ProjectService;
import com.baomidou.springwind.service.WeekOrderService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.text.DecimalFormat;
import java.util.*;

/**
 * <p>
 * 城市配送:订单分析
 * </p>
 *
 * @Author huangtao
 * @Date 2017-8-21
 */
@Controller
@RequestMapping("/network/city")
public class FlowAnalysisController extends BaseController {

    @Autowired
    private FlowAnalysisService flowAnalysisService;
    @Autowired
    private ProjectService projectService;
    @Autowired
    private WeekOrderService weekOrderService;
    /**
     * 城市数据导入--客户分析数据--显示医院
     */
    @RequestMapping("/flow/list")
    @Permission("1001")
    public String customList(Model model) {
        EntityWrapper<Project> wrapper = new EntityWrapper<Project>();
        wrapper.eq("operator_id",getCurrentUserId());
        wrapper.in("state", Arrays.asList("1", "2"));
        model.addAttribute("prjList", projectService.selectList(wrapper));
        return pjaxPath("/file/flow_analysis_list", model);
    }

    /**
     * 城市数据导入--客户分析数据--ajax获取数据
     */
    @ResponseBody
    @RequestMapping("/flow/getData")
    @Permission("1001")
    public String getData(Page<FlowAnalysis> page,String batchId) {
        page = new Page<FlowAnalysis>(page.getCurrent(), page.getSize());
        EntityWrapper<FlowAnalysis> wrapper = new EntityWrapper<FlowAnalysis>();
        if (StringUtils.isNotEmpty(batchId)){
            wrapper.eq("batch_id",batchId);
        }
        wrapper.eq("operator_id", getCurrentUserId());
        wrapper.orderBy("create_time", false);
        page = flowAnalysisService.selectPage(page, wrapper);
        List<FlowAnalysis> flowAnalysisList = page.getRecords();
        if (flowAnalysisList.size() > 0) {
            Project project = projectService.selectById(flowAnalysisList.get(0).getBatchId());
            for (FlowAnalysis flowAnalysis : flowAnalysisList) {
                flowAnalysis.setBatchName(project == null ? "--" : project.getName());
            }
        }
        return jsonPage(page);
    }

    /**
     * 客户分析主界面
     */
    @RequestMapping("/flow")
    @Permission("1001")
    public String customGroup(Model model) {
        EntityWrapper<WeekOrder> wrapper = new EntityWrapper<WeekOrder>();
        wrapper.eq("batch_id",getProjectId());
        wrapper.eq("operator_id", getCurrentUserId());
        model.addAttribute("prjList", weekOrderService.selectList(wrapper));
        return pjaxPath("/lpp/network/city/flow/main", model);
    }

    /**
     * 物流量分析--医院洁衣架数量分布图
     */
    @ResponseBody
    @RequestMapping("/flow/bar1")
    @Permission("1001")
    public String  flowBybarClean() throws Exception{
        int selectCount=5;
        EntityWrapper<FlowAnalysis> wrapper = new EntityWrapper<FlowAnalysis>();
        wrapper.eq("batch_id", getProjectId());
        wrapper.eq("operator_id", getCurrentUserId());
        final List<FlowAnalysis> flowAnalyses = flowAnalysisService.selectList(wrapper);
        int minCount=0,maxCount=0,difference=0;
        int countlist[]=new int[flowAnalyses.size()];
        //建立一个数组
        for (int i=0;i<flowAnalyses.size();i++){
            countlist[i]=flowAnalyses.get(i).getCleanClothes();
        }
        Arrays.sort(countlist);
        maxCount=countlist[countlist.length-1];
        minCount=countlist[0];
        difference=(maxCount-minCount)/selectCount;
        JSONObject ret = new JSONObject();
        //找到t需要的数据
        JSONArray t = new JSONArray();
        for (int i=0;i<selectCount-1;i++){
            t.add("["+(minCount+difference*i)+","+(minCount+difference*(i+1))+"]");
        }
        t.add("["+(maxCount-difference)+","+maxCount+"]");
        //找到a1需要的数据
        JSONArray a1 = new JSONArray();
        int number1=0,number2=0;
        for (int i=0;i<selectCount-1;i++){
            for(int j=0;j<countlist.length;j++){
                if((minCount+difference*i)<=countlist[j]&&countlist[j]<(minCount+difference*(i+1))){
                    number1++;
                }
            }
            a1.add(number1);
            number2=number1+number2;
            number1=0;
        }
        a1.add(countlist.length-number2);

        ret.put("x",t);
        ret.put("s1",a1);
        return callbackSuccess(ret);

    }

    /**
     * 物流量分析--医院污衣架数量分布图
     */
    @ResponseBody
    @RequestMapping("/flow/bar2")
    @Permission("1001")
    public String  flowByDirty() throws Exception{
        int selectCount=6;
        EntityWrapper<FlowAnalysis> wrapper = new EntityWrapper<FlowAnalysis>();
        wrapper.eq("batch_id", getProjectId());
        wrapper.eq("operator_id", getCurrentUserId());
        final List<FlowAnalysis> flowAnalyses = flowAnalysisService.selectList(wrapper);
        int minCount=0,maxCount=0,difference=0;
        int countlist[]=new int[flowAnalyses.size()];
        //建立一个数组
        for (int i=0;i<flowAnalyses.size();i++){
                countlist[i]=flowAnalyses.get(i).getUnCleanClothes();
        }
        Arrays.sort(countlist);
        maxCount=countlist[countlist.length-1];
        minCount=countlist[0];
        difference=(maxCount-minCount)/selectCount;
        JSONObject ret = new JSONObject();
        //找到t需要的数据
        JSONArray t = new JSONArray();
        for (int i=0;i<selectCount-1;i++){
            t.add("["+(minCount+difference*i)+","+(minCount+difference*(i+1))+"]");
        }
        t.add("["+(maxCount-difference)+","+maxCount+"]");
        //找到a1需要的数据
        JSONArray a1 = new JSONArray();
        int number1=0,number2=0;
        for (int i=0;i<selectCount-1;i++){
            for(int j=0;j<countlist.length;j++){
                if((minCount+difference*i)<=countlist[j]&&countlist[j]<(minCount+difference*(i+1))){
                    number1++;
                }
            }
            a1.add(number1);
            number2=number1+number2;
            number1=0;
        }
        a1.add(countlist.length-number2);
        ret.put("x",t);
        ret.put("s1",a1);
        return callbackSuccess(ret);
    }


    /**
     * 物流量分析--每天的洗涤量分布图
     */
    @ResponseBody
    @RequestMapping("/flow/bar3")
    @Permission("1001")
    public String  flowBybarCount() throws Exception{
        EntityWrapper<FlowAnalysis> wrapper = new EntityWrapper<FlowAnalysis>();
        wrapper.eq("batch_id", getProjectId());
        wrapper.eq("operator_id", getCurrentUserId());
        final List<FlowAnalysis> flowAnalyses = flowAnalysisService.selectList(wrapper);
        int countlist[]=new int[6];
        //建立一个数组
        countlist[0]=0;
        countlist[1]=0;
        countlist[2]=0;
        countlist[3]=0;
        countlist[4]=0;
        countlist[5]=0;
        JSONObject ret = new JSONObject();
        //找到t需要的数据
        JSONArray t = new JSONArray();
        for (int i=1;i<=6;i++){
            t.add("周"+i);
        }
        //找到a1需要的数据
        JSONArray a1 = new JSONArray();
        for (int i=0;i<6;i++){
            a1.add(countlist[i]);
        }
        ret.put("x",t);
        ret.put("s1",a1);
        return callbackSuccess(ret);
    }
}
